from d2l import torch as d2l
import matplotlib.pyplot as plt
import copy
import collections
from cizhui_chuli_gru import new_contents

print(new_contents)


class Vocab:  # @save
    """文本词表"""

    def __init__(self, tokens=None, min_freq=0, reserved_tokens=None):
        if tokens is None:
            tokens = []
        if reserved_tokens is None:
            reserved_tokens = []
        # 按出现频率排序
        counter = count_corpus(tokens)
        self._token_freqs = sorted(counter.items(), key=lambda x: x[1],
                                   reverse=True)
        # 未知词元的索引为0
        self.idx_to_token = ['<unk>'] + reserved_tokens
        self.token_to_idx = {token: idx
                             for idx, token in enumerate(self.idx_to_token)}
        for token, freq in self._token_freqs:
            if freq < min_freq:
                break
            if token not in self.token_to_idx:
                self.idx_to_token.append(token)
                self.token_to_idx[token] = len(self.idx_to_token) - 1

    def __len__(self):
        return len(self.idx_to_token)

    def __getitem__(self, tokens):
        if not isinstance(tokens, (list, tuple)):
            return self.token_to_idx.get(tokens, self.unk)
        return [self.__getitem__(token) for token in tokens]

    def to_tokens(self, indices):
        if not isinstance(indices, (list, tuple)):
            return self.idx_to_token[indices]
        return [self.idx_to_token[index] for index in indices]

    @property
    def unk(self):  # 未知词元的索引为0
        return 0

    @property
    def token_freqs(self):
        return self._token_freqs


def count_corpus(tokens):  # @save
    """统计词元的频率"""
    # 这里的tokens是1D列表或2D列表
    if len(tokens) == 0 or isinstance(tokens[0], list):
        # 将词元列表展平成一个列表
        tokens = [token for line in tokens for token in line]
    return collections.Counter(tokens)


def extend_data(contents):
    _new_contents = []
    for c in contents:
        _new_contents.append(c)
        # if "number" in c[0]:
        #     tmp_a = copy.deepcopy(c)
        #     for i in range(1,101):
        #         tmp_b = copy.deepcopy(tmp_a)
        #         tmp_b[0][tmp_b[0].index("number")] = str(i)
        #         _new_contents.append(tmp_b)
        # else:
        #     _new_contents.append(c)
    print(new_contents)
    return _new_contents


# 8.2.4. 整合所有功能
def load_corpus_time_machine(max_tokens=-1):  # @save
    """返回时光机器数据集的词元索引列表和词表"""
    n_contents = extend_data(new_contents)
    tokens = [i[0] for i in n_contents]
    # print(len(tokens))
    vocab = Vocab(tokens)
    corpus = [vocab[token] for line in tokens for token in line]
    if max_tokens > 0:
        corpus = corpus[:max_tokens]
    return corpus, vocab, n_contents

# train_iter, vocab,_ = load_corpus_time_machine()
# print(vocab['number', '個', '附加', '的', '天賦', '為', '火焰', '包覆'])
# print(len(vocab))
